package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;


/**
 * Servlet implementation class GetDataServlet
 */
@WebServlet("/GetDataServlet")
public class GetDataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetDataServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    	Connection connection = null;
    	try {
    		Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java", "root", "root");
	    	
			ResultSet resultSet = null;
			Statement statement = connection.createStatement();
			resultSet = statement.executeQuery("select max(id) from grade");
			int maxn = 0;
			while (resultSet.next()) {
				maxn = resultSet.getInt(1);
			}
			double[] stuScore = new double[maxn], aver = new double[maxn];
			
			
			String username = (String)request.getSession().getAttribute("username");
	//        Integer[] salesVolume = {10,100,20,56,35,80}; 
	    	String sql = "select * from grade where no = ? order by id";
	        PreparedStatement preparedStatement = null;
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, username);
			resultSet = preparedStatement.executeQuery();
			
			int i = 0;
			while (resultSet.next()) {
				stuScore[i++] = resultSet.getDouble("score");
			}
			
			for (i = 0; i < stuScore.length; i++) {
				System.out.println(stuScore[i]);
			}

			String[] months = {"1","2","3","4","5", "6","7","8","9","10","11","12","13","14"}; 
			
			preparedStatement = connection.prepareStatement("select avg(score) from grade where id = ?");
			
			DecimalFormat df= new DecimalFormat("#.##");
			for (i = 1; i <= maxn; i++) {
				preparedStatement.setInt(1, i);
				resultSet = preparedStatement.executeQuery();
				while (resultSet.next()) {
					aver[i - 1] = Double.parseDouble(df.format(resultSet.getDouble(1)));
				}
			}
			Map<String, Object> map = new HashMap<>();  
//     	    map.put("salesVolume", salesVolume);  
			map.put("stuScore",stuScore);  
			map.put("months", months); 
			map.put("aver", aver);
			response.getWriter().println(JSON.toJSONString(map)); 
			preparedStatement.close();
			statement.close();
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			try {
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
			
        
          
    }  
  
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        doGet(request, response);  
    }  
}
